<template>
  <div></div>
</template>

<script>
  import Loading from '@/vmui/dialog/src/loading'
  export default {
    methods: {
      usage1 () {
        let loading1 = Loading.new({message: '很快就好了', callback: () => { console.log('closed') }})
        setTimeout(function () {
          loading1.close()
        }, 3000)
      },
      // new 几个相同的identify，就需要调用多少次 close 方法
      usage2 () {
        Loading.new({message: '很快就好了', callback: () => { console.log('closed') }, identify: '1'})
        Loading.new({identify: '1'})
        Loading.new({identify: '1'})
        Loading.new({identify: '1'})
        Loading.new({identify: '1'})
        function setTime () {
          setTimeout(function () {
            let identify1Loading = Loading.getWithIdentify('1')
            // 强制关闭所有
            // Loading.getWithIdentify('1').forceClose()
            if (identify1Loading) {
              console.log('before close: ', identify1Loading.$extraCounter)
              identify1Loading.close()
              console.log('after close: ', identify1Loading.$extraCounter)
              setTime()
            }
          }, 3000)
        }
        setTime()
      },
      usage3 () {
        function createPromise () {
          return new Promise((resolve, reject) => {
            setTimeout(() => {
              let random = Math.random()
              if (random > 0.5) {
                resolve('哈哈，赢了')
              }
              reject(new Error('呜呜~，输了'))
            }, 3000)
          })
        }
        Loading.newWithPromise(createPromise(), {message: '华山论剑'}).then(result => {
          console.log(result)
        }).catch(err => {
          console.log('err--:', err)
        })
      }
    },
    mounted () {
//      this.usage1()
//      this.usage2()
      this.usage3()
    }
  }
</script>
